<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <el-card class="box-card">
      <el-form
        ref="formList"
        :inline="true"
        class="demo-form-inline"
        label-width="80px"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="车型编号">
              <el-input
                v-model="id"
                placeholder="请输入车型编号"
                style="width: 250px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载重量">
              <el-select
                v-model="allowableLoad"
                placeholder="请输入应载重量"
                style="width: 250px"
              >
                <el-option
                  v-for="item in carAllowableLoad"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载体积">
              <el-select
                v-model="allowableVolume"
                placeholder="请输入应载体积"
                style="width: 250px"
              >
                <el-option
                  v-for="item in carAllowableVolume"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="车辆类型">
          <el-input
            v-model="name"
            placeholder="请输入车辆类型"
            style="width: 250px"
            class="el-btn"
          ></el-input>
        </el-form-item>
        <el-button
          class="btn"
          @click="searchVal"
        >搜索</el-button>
        <el-button @click="resetting">重置</el-button>
      </el-form>
    </el-card>

    <el-card
      v-loading="loading"
      class="el-card"
    >
      <el-button
        class="btn"
        @click="openDialog"
      >新增车型</el-button>
      <el-table
        class="tab"
        :data="list"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          width="180"
          prop="id"
          type="index"
          :index="isIndex"
        >
        </el-table-column>
        <el-table-column
          label="车型编号"
          width="180"
          prop="id"
        >
        </el-table-column>
        <el-table-column
          label="车辆类型"
          prop="name"
        > </el-table-column>
        <el-table-column
          label="车型数量"
          prop="num"
        > </el-table-column>
        <el-table-column
          label="应载重量(吨)"
          prop="allowableLoad"
        >
        </el-table-column>
        <el-table-column
          label="应载体积(立方米)"
          prop="allowableVolume"
        >
        </el-table-column>
        <el-table-column
          label="长(米)"
          prop="measureLong"
        > </el-table-column>
        <el-table-column
          label="宽(米)"
          prop="measureWidth"
        > </el-table-column>
        <el-table-column
          label="操作"
          class="tabCol"
        >
          <template #default="{ row }">
            <el-button
              type="text"
              class="edit"
              style="margin-left: -50px"
              @click="editList(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              class="del"
              @click="delList(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        background=""
        class="pag"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      :title="title"
      :visible="isDialog"
      :close-on-click-modal="false"
      width="35%"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        label-width="80px"
        label="right"
        :model="form"
        :rules="rules"
      >
        <el-form-item
          label="车辆类型"
          prop="name"
        >
          <el-input
            v-model="form.name"
            placeholder="请输入车辆类型"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="车载重量"
          prop="allowableLoad"
        >
          <el-input
            v-model="form.allowableLoad"
            placeholder="请输入车载重量"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >吨</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="应载体积"
          prop="allowableVolume"
        >
          <el-input
            v-model="form.allowableVolume"
            placeholder="请输入应载体积"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >立方</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="长"
          prop="measureLong"
        >
          <el-input
            v-model="form.measureLong"
            placeholder="请输入长"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >米</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="宽"
          prop="measureWidth"
        >
          <el-input
            v-model="form.measureWidth"
            placeholder="请输入宽"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >米</i>
          </el-input>
        </el-form-item>
        <el-form-item
          label="高"
          prop="measureHigh"
        >
          <el-input
            v-model="form.measureHigh"
            placeholder="请输入高"
          >
            <i
              slot="suffix"
              class="i-ipt"
            >米</i>
          </el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="btn"
          @click="addForm"
        >确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { truckTypeCommonList } from '@/api/common'
import { list, add, del, detail, update } from '@/api/transit'
export default {
  data() {
    return {
      loading: false,
      list: [],
      form: {
        allowableLoad: '', // 准载重量
        allowableVolume: '', // 准载体积
        id: '',
        goodsTypes: [],
        measureHigh: '', // 高
        measureLong: '', // 长
        measureWidth: '', // 宽
        name: '', // 车辆类型名称
        num: '' // 车型数量
      },

      total: 0,
      page: 1,
      pageSize: 10,
      isDialog: false,
      rules: {
        name: [
          { required: true, message: '车型名称不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '准载重量不能为空', trigger: 'blur' },
          {
            pattern: /^([1-9][0-9]{0,1}|100)$/,
            message: '只能输入1-100之间得正整数',
            trigger: 'blur'
          }
        ],

        allowableVolume: [
          { required: true, message: '准载体积不能为空', trigger: 'blur' },
          {
            pattern: /^([1-9][0-9]{0,1}|100)$/,
            message: '只能输入1-100之间得正整数',
            trigger: 'blur'
          }
        ],
        measureHigh: [
          { required: true, message: '准载体积不能为空', trigger: 'blur' },
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(.\d{1,2})?$/,
            message: '只能输入数字,最多保留两位小数',
            trigger: 'blur'
          }
        ],
        measureLong: [
          { required: true, message: '准载体积不能为空', trigger: 'blur' },
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(.\d{1,2})?$/,
            message: '只能输入数字,最多保留两位小数',
            trigger: 'blur'
          }
        ],
        measureWidth: [
          { required: true, message: '准载体积不能为空', trigger: 'blur' },
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(.\d{1,2})?$/,
            message: '只能输入数字,最多保留两位小数',
            trigger: 'blur'
          }
        ]
      },

      allowableLoad: '',
      allowableVolume: '',
      id: '',
      name: '',

      carAllowableLoad: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      carAllowableVolume: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ]
    }
  },
  computed: {
    title() {
      if (this.form.id) {
        return '编辑车辆'
      } else {
        return '添加车辆'
      }
    }
  },
  created() {
    // this.getCarModel()
    this.getList()
  },
  methods: {
    /**
     * 搜索功能
     * 里面搜索的数据在列表渲染里执行
     */
    async searchVal() {
      this.getList()
    },
    /**
     * 重置
     */
    resetting() {
      this.allowableLoad = ''
      this.allowableVolume = ''
      this.id = ''
      this.name = ''
      this.getList()
    },
    /**
     * 编辑
     */
    async editList(id) {
      this.isDialog = true
      const res = await detail(id)
      // console.log(res)
      this.form = res.data
    },
    /**
     * 删除
     */
    delList(id) {
      this.$confirm('您确定要永久删除此条数据吗?', '提示')
        .then(async () => {
          await del(id)
          this.$message.success('删除成功')
          if (this.list.length === 1 && this.pageSize > 1) {
            this.page--
          }
          this.getList()
        })
        .catch(() => {})
    },
    /**
     * 添加
     */
    addForm() {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          this.$message.success('编辑成功')
        } else {
          await add(this.form)
          // console.log(res)
          this.$message.success('添加成功')
        }

        this.closeDialog()
        this.getList()
      })
    },
    /**
     * 关闭弹框
     */
    closeDialog() {
      this.isDialog = false
      this.$refs.form.resetFields()
    },
    /**
     * 打开弹框
     */
    openDialog() {
      this.isDialog = true
      this.form.id = ''
    },
    /**
     * 每页展示页码数
     */
    handleSizeChange(val) {
      this.pageSize = val
      // this.page = 1
      this.getList()
    },
    // 当前页
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    /**
     * 渲染列表
     */
    async getList() {
      this.loading = true
      const res = await list({
        page: this.page,
        pageSize: this.pageSize,
        allowableLoad: this.allowableLoad,
        allowableVolume: this.allowableVolume,
        id: this.id,
        name: this.name
      })
      // console.log(res)
      this.list = res.data.items
      // console.log(res.data.counts)
      this.total = +res.data.counts
      this.loading = false
    },
    /**
     * 处理序号
     */
    isIndex(index) {
      return (this.page - 1) * this.pageSize + index + 1
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.add-btn {
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }

  .el-form--inline {
    margin-top: 30px;
  }
  .btn {
    background-color: #e15536;
    color: #fff;
  }
  .el-card {
    margin-top: 20px;
    margin-bottom: 50px;
  }
  .el-btn {
    margin-right: 50px;
  }
  .tab {
    margin-top: 30px;
  }
  .pag {
    text-align: center;
  }
  .del {
    font-size: 12px;
    color: #f56c6c;
  }
  .edit {
    font-size: 12px;
    color: #419eff;
  }
  .dialog-footer {
    text-align: center;
  }
  .i-ipt {
    color: #000;
    font-style: normal;
    margin-right: 10px;
  }
  .tem-def {
    text-align: left;
  }
}
</style>
